package com.xingxue.logistics.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xingxue.logistics.common.dto.SysRole;
import com.xingxue.logistics.common.dto.SysRoleExample;
import com.xingxue.logistics.dao.mapper.SysRoleMapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Kris at 2018/10/9 16:14
 */
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    SysRoleMapper roleMapper;


    @Override
    public PageInfo<SysRole> searchPage(Integer id, String name, Integer type, Integer pageNum, Integer pageSize) {
        SysRoleExample example = new SysRoleExample();
        example.setOrderByClause("id asc");
        SysRoleExample.Criteria criteria = example.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        if(StringUtils.isNotBlank(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(type != null){
            criteria.andTypeEqualTo(type);
        }

        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysRoles = roleMapper.selectByExample(example);
        return new PageInfo<SysRole>(sysRoles);
    }

    @Override
    public int add(SysRole role) {
        return roleMapper.insertSelective(role);
    }


    @Override
    public int update(SysRole role) {
        return roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public int delete(Integer id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SysRole> loadAll() {
        SysRoleExample example = new SysRoleExample();
        example.setOrderByClause("id asc");
        return roleMapper.selectByExample(example);
    }

    @Override
    public List<SysRole> search(Integer id, String name, Integer type) {
        return roleMapper.search(id,name,type);
    }
}
